import Vue from 'vue';
import HomeView from "./index.vue";
import mtracker from "@src/util/mtracker";
// import initData from "@src/modules/productV2/productEdit/initData.js"

import { Loading } from 'element-ui';
import { getAsyncProductV2ProductEditInitData, getAsyncProductV2CreateInitData, getAsyncProductV2EditInitData, getAsyncProductV2RegisterCreateInitData, getAsyncProductV2RegisterEditInitData, getAsyncProductV2EditModalOnTaskInitData } from '@src/api/InitDataApi'
import qs from 'qs';
import platform from '@src/platform';

mtracker();

const componentItem = (initData) => {
  return {
    provide: {
      initData: Object.freeze(initData)
    },
    render(h) {
      return h(HomeView);
    }
  }
}

Vue.component('async-component', function(resolve) {
  
  // 需要加loading 打开此注释
  // const instance = Loading.service({ fullscreen: true });
  
  let query = qs.parse(window.location.search.substr(1));
  let params = Object.assign({}, query, {})
  const pathname = window.location.pathname
  
  let fetchInitData = null
  if(pathname.indexOf('/customer/product/create') >= 0) {
    const customerId = window.location.pathname.split('/customer/product/create/')[1];
    params = Object.assign({}, {customerId}, params)
    fetchInitData = getAsyncProductV2CreateInitData
  } else if(pathname.indexOf('/customer/product/edit/') >= 0) {
    const id = window.location.pathname.split('/customer/product/edit/')[1];
    params = Object.assign({}, params, {id})
    fetchInitData = getAsyncProductV2EditInitData
  } else if(pathname.indexOf('/customer/product/register/create') >= 0) {
    const registerId = window.location.pathname.split('/customer/product/register/create/')[1];
    params = Object.assign({}, params, {registerId})
    fetchInitData = getAsyncProductV2RegisterCreateInitData
  } else if(pathname.indexOf('/customer/product/register/edit/') >= 0) {
    const ids = pathname.split('/customer/product/register/edit/')[1];
    params = Object.assign({}, params, {
      id: ids && ids.split('/')[0] || '',
      registerId: ids && ids.split('/')[1] || ''
    })
    fetchInitData = getAsyncProductV2RegisterEditInitData
  } else if(pathname.indexOf('/product/createOnTask') >= 0) {
    const customerId = window.location.pathname.split('/product/createOnTask/')[1];
    params = Object.assign({}, params, {customerId})
    fetchInitData = getAsyncProductV2EditModalOnTaskInitData
  } 
  
  fetchInitData(params).then(result => {
    let initJSON = ''
    try {
      if(result && result.data && result.data.initJson) {
        initJSON = typeof(result.data.initJson) === 'string' ? JSON.parse(result.data.initJson) : result.data.initJson
      }
    } catch (error) {
      console.error('initJSON 解析失败')
    }

    const initData = initJSON || {}
    const title = (result && result.data && result.data.title) || ''
    const isSuccess = result.succ || result.success
    
    if (!isSuccess) {
      window.location.href = '/500'
      return 
    }
    
    try {
      window._init = JSON.stringify(initData)
    } catch (error) {
      console.error('initData 赋值失败')
    }
    
    // 需要加loading 打开此注释
    // instance.close();
    
    if (window.frameElement) {
      const currentTabId = window.frameElement.dataset.id;
      platform.setTabTitle({
        id: currentTabId,
        title
      })
    }
    resolve(componentItem(initData))
    
  }).catch(error => {
    console.error(error)
    window.location.href = '/500'
  })
  
});

let app = {
  render(h) {
    return <async-component />
  }
}

export default app;